package loengud.w09;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LayoutExample extends JFrame {

	public LayoutExample() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600, 600);
		// content pane will have border layout
		getContentPane().setLayout(new BorderLayout());
		
		// left menu
		JPanel leftPanel = new JPanel();
		leftPanel.setBackground(Color.GRAY);
		// buttons will be placed on top of each other
		// in two columns
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.X_AXIS));
		
		// left column
		JPanel leftButtonColumn = new JPanel();
		leftButtonColumn.setOpaque(false); // transparent background
		leftButtonColumn.setLayout(new BoxLayout(leftButtonColumn, BoxLayout.Y_AXIS));
		
		// let's add buttons
		JButton b1 = new JButton("add car");
		JButton b2 = new JButton("add bus");
		JButton b3 = new JButton("add atv");
		// let's align buttons
		b1.setAlignmentX(0);
		b2.setAlignmentX(0.5f);
		b3.setAlignmentX(1);
		leftButtonColumn.add(b1);
		// add some space between buttons
		leftButtonColumn.add(Box.createRigidArea(new Dimension(0, 5)));
		leftButtonColumn.add(b2);
		leftButtonColumn.add(Box.createRigidArea(new Dimension(0, 5)));
		leftButtonColumn.add(b3);
		
		// let's add button column panel to the left panel
		leftPanel.add(leftButtonColumn);
		// add some space between button rows
		leftPanel.add(Box.createRigidArea(new Dimension(5, 0)));
		
		// right column
		JPanel rightButtonColumn = new JPanel();
		rightButtonColumn.setOpaque(false); // transparent background
		rightButtonColumn.setLayout(new BoxLayout(rightButtonColumn, BoxLayout.Y_AXIS));
		
		// let's add buttons
		JButton b4 = new JButton("remove car");
		JButton b5 = new JButton("remove bus");
		JButton b6 = new JButton("remove atv");
		// align buttons
		b4.setAlignmentX(0.3f);
		b5.setAlignmentX(0.2f);
		b6.setAlignmentX(0.1f);
		rightButtonColumn.add(b4);
		rightButtonColumn.add(Box.createRigidArea(new Dimension(0, 5)));
		rightButtonColumn.add(b5);
		rightButtonColumn.add(Box.createRigidArea(new Dimension(0, 5)));
		rightButtonColumn.add(b6);
		
		// let's add button column panel to the left panel
		leftPanel.add(rightButtonColumn);
		leftPanel.add(Box.createRigidArea(new Dimension(5, 0)));
		
		
		
		getContentPane().add(leftPanel, BorderLayout.WEST);
		
		// top pane
		
		JPanel top = new JPanel();
		top.setBackground(Color.LIGHT_GRAY);
		// flow is default anyway
		top.setLayout(new FlowLayout(FlowLayout.RIGHT));
		top.add(new JTextField("Hello"));
		top.add(new JLabel("world"));
		top.add(new JButton("peace"));
		
		getContentPane().add(top, BorderLayout.NORTH);
		
		// center pane, grid bag layout
		JPanel centerPanel = new JPanel();
		addComponentsToPane(centerPanel);
		getContentPane().add(centerPanel, BorderLayout.CENTER);
		
	}
	public static void main(String[] args) {
		LayoutExample f = new LayoutExample();
		f.setVisible(true);

	}

	public void addComponentsToPane(JPanel pane) {

		boolean shouldFill = true;
		boolean shouldWeightX = true;
		
		JButton button;
		pane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		if (shouldFill) {
			// natural height, maximum width
			c.fill = GridBagConstraints.HORIZONTAL;
		}

		button = new JButton("Button 1");
		if (shouldWeightX) {
			c.weightx = 0.5;
		}
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		pane.add(button, c);

		button = new JButton("Button 2");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 0;
		pane.add(button, c);

		button = new JButton("Button 3");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 2;
		c.gridy = 0;
		pane.add(button, c);

		button = new JButton("Long-Named Button 4");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 40; // make this component tall
		c.weightx = 0.0;
		c.gridwidth = 3;
		c.gridx = 0;
		c.gridy = 1;
		pane.add(button, c);

		button = new JButton("5");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 0; // reset to default
		c.weighty = 1.0; // request any extra vertical space
		c.anchor = GridBagConstraints.PAGE_END; // bottom of space
		c.insets = new Insets(10, 0, 0, 0); // top padding
		c.gridx = 1; // aligned with button 2
		c.gridwidth = 2; // 2 columns wide
		c.gridy = 2; // third row
		pane.add(button, c);
	}

}
